package com.wkw.jkyf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkw.jkyf.common.ResultMapUtil;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Supplier;
import com.wkw.jkyf.service.IDruginfoService;
import com.wkw.jkyf.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


//药品
@Controller
@RequestMapping(value = "/druginfo")
public class DruginfoController {
    /*
转向药品页面
*/
    @Autowired
    private IDruginfoService druginfoService;

    @RequestMapping
    public String druginfo(){
        return "/druginfo";
    }

    /*
    分页查询供应商列表
     */
    @RequestMapping("/druginfoQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize){
        try{
            IPage<Druginfo> iPage = druginfoService.selectDruginfoPage(page,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtil.getHashException(e);
        }

    }
    //跳转到添加页面
    @RequestMapping(value = "/druginfoPage")
    public String supplierPage(){
        return "/druginfoPage";
    }

    //添加
    @RequestMapping(value = "/druginfoAdd")
    @ResponseBody
    public Object druginfoAdd(Druginfo druginfo){
        try {
            int i = druginfoService.addDruginfo(druginfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashException(e);
        }

    }

    //按id查询
    @RequestMapping(value = "/druginfoQueryById")
    public String druginfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model) {
        Druginfo druginfo= druginfoService.queryDruginfoById(id);
        model.addAttribute("obj",druginfo);
        return "/druginfoPage";
    }

    //修改一个供应商
    @RequestMapping(value = "/druginfoEdit")
    @ResponseBody
    public Object druginfoEdit(Druginfo druginfo) {
        try {
            int i= druginfoService.editDruginfo(druginfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }

    // 删除一个供应商
    @RequestMapping(value = "/druginfoDelById")
    @ResponseBody
    public Object druginfoDelById(Integer id) {
        try {
            int i= druginfoService.deleteDruginfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }
    //获取所有药品

    @RequestMapping(value = "/druginfoList")
    @ResponseBody
    public Object supplierList(){
        List<Druginfo> druginfos = druginfoService.queryDruginfoList();
        return ResultMapUtil.getHashMapMyList(druginfos);
    }

    //转向药品保质期检查页面
    @RequestMapping("/warranty")
    public String warranty(){
        return "/warranty";
    }

    //按药品名称查询编号
    @RequestMapping(value = "/drugInfoByCondition")
    @ResponseBody
    public Object drugNumberList(@RequestParam(value = "drugName",required = false)String drugName,
                                 @RequestParam(value = "number",required = false)String number){
        List<Druginfo> druginfos = druginfoService.selectDrugNumberByName(drugName,number);
        return ResultMapUtil.getHashMapMyList(druginfos);
    }





}
